package com.icehand.permission.interceptor;

import com.icehand.permission.annotation.PermissionCheck;
import com.icehand.permission.exception.PermissionException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.lang.reflect.Method;

/**
 * @author icehand
 */
@Aspect
public class PermissionMethodInterceptor {
    private final StringRedisTemplate redisTemplate;

    public PermissionMethodInterceptor(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Around("execution(public * *(..)) && @annotation(com.icehand.permission.annotation.PermissionCheck)")
    public Object interceptor(ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        PermissionCheck lock = method.getAnnotation(PermissionCheck.class);

        //TODO:去校验方法是否有权限，包括功能和数据权限

        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throw new PermissionException(throwable);
        }
    }
}